package com.wly.frame.mvvm.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.wly.frame.R
import me.xdj.view.SimpleMultiStateView

/**
 * author: wangliyun
 * date: 2022/3/23
 * description:
 */
abstract class BaseFragment : Fragment() {

    lateinit var mContext: Context

    protected var mActivity: Activity? = null

    protected var multiStateView: SimpleMultiStateView? = null

    private var viewBinding: ViewBinding? = null
    private var viewRoot: View? = null

    private var isLoad = false

    private var isInit = false

    private var isFirstLoad = true


    override fun onAttach(context: Context) {
        super.onAttach(context)
        this.mContext = context
        this.mActivity = context as? BaseActivity
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initView(view, savedInstanceState)
        initData()
        isInit = true
        /*初始化的时候去加载数据**/
        isCanLoadData()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        val rootView: View = inflater.inflate(R.layout.fragment_base, container, false)
        val statusView = rootView.findViewById<FrameLayout>(R.id.statusView)

        multiStateView = SimpleMultiStateView(mContext)

        if (getViewBinding(inflater, container) != null) {
            viewBinding = getViewBinding(inflater, container)
            viewRoot = viewBinding?.root
            multiStateView?.addView(viewRoot)
        } else {
            viewRoot = View.inflate(mContext, getLayoutRes(), multiStateView)
        }
        statusView.addView(multiStateView)

        return rootView
    }


    override fun onResume() {
        super.onResume()
        if (isFirstLoad) {
            //适用于viewpager2
            loadData2()
            isFirstLoad = false
        }
    }

    abstract fun getLayoutRes(): Int

    abstract fun initData()

    abstract fun initView(view: View, savedInstanceState: Bundle?)

    abstract fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): ViewBinding?


    /**
     * 是否可以加载数据
     * 可以加载数据的条件：
     * 1.视图已经初始化
     * 2.视图对用户可见
     */
    private fun isCanLoadData() {
        if (!isInit) {
            return
        }
        if (userVisibleHint && !isLoad) {
            loadData()
            isLoad = true
        } else {
            if (isLoad) {
                stopLoad()
            }
        }
    }

    /**
     * viewpager+fragment 停止加载
     */
    protected open fun stopLoad() {}

    // viewPager + Fragment lazy load
    protected open fun loadData() {

    }

    // viewPager2+Fragment lazy load
    protected open fun loadData2() {

    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        isInit = false
        isLoad = false

        if (viewBinding != null) {
            viewBinding = null
        }
    }
}